package com.cxb.receiver;

import com.cxb.MainActivity;
import com.cxb.entity.GpsDestinationInfo;
import com.cxb.manager.NavigationManager;
import com.cxb.utils.ApnUtility;
import com.cxb.utils.CommonUtils;
import com.cxb.utils.Config;
import com.cxb.utils.Tts;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.PowerManager;
import android.util.Log;

public class ECardReceiver extends BroadcastReceiver {
	/** 方案商接收翼卡的广播所使用的Action名称 **/
	public static String _Action_ = "com.android.ecar.send";
	/** CMD参数所用的标示 **/
	public static String _CMD_ = "ecarSendKey";
	/** TYPE参数的标示 **/
	public static String _TYPE_ = "cmdType";
	/** _TYPE_ = “standCMD”时为普通广播 **/
	public static String _TYPE_STANDCMD_ = "standCMD";
	// 所传递的参数列表，每个参数名会用逗号分开，需要解释出参数名，
	// 再以此作为Key取出数据
	public static String _KEYS_ = "keySet";

	@Override
	public void onReceive(Context context, Intent intent) {

		Log.i("ECardReceiver",
				intent.getAction() + " cmd=" + intent.getStringExtra(_CMD_));
		if (intent.getAction().equals(_Action_) && Config.isEcardSupport(context)) {

			if ("StartMap".equals(intent.getStringExtra(_CMD_))) {
				// 收到导航命令

				String poiName = intent.getStringExtra("poiName");
				// 高德
				String lat = intent.getStringExtra("gaode_latitude");
				String lng = intent.getStringExtra("gaode_longitude");

				GpsDestinationInfo gpsDestinationInfo = new GpsDestinationInfo();
				gpsDestinationInfo.destName = poiName;
				gpsDestinationInfo.latitude = Double.parseDouble(lat);
				gpsDestinationInfo.longitude = Double.parseDouble(lng);
				new NavigationManager(context)
						.startupNavigation(gpsDestinationInfo);

			} else if ("SetAPN".equals(intent.getStringExtra(_CMD_))) {
				// 设置vpn
				String apn = intent.getStringExtra("APN");
				String mnc = intent.getStringExtra("MNC");
				String mcc = intent.getStringExtra("MCC");

				try {
					new ApnUtility(context).editApn("eCard", apn, mcc, mnc);
				} catch (Exception e) {
					e.printStackTrace();
				}

			} else if ("TTSSpeak".equals(intent.getStringExtra(_CMD_))) {
				// 播报tts
				final String text = intent.getStringExtra("text");
				Log.i("ECardReceiver", "播报tts text=" + text);
				PowerManager mPowerManager;
				mPowerManager = (PowerManager) context
						.getSystemService(Context.POWER_SERVICE);
				if (mPowerManager.isScreenOn()) {
					if (text != null && MainActivity.tts != null) {
						Tts.speak(text);
					}
				} else {
					CommonUtils.wakeUpScreen(context);
					new Handler().postDelayed(new Runnable() {

						@Override
						public void run() {
							if (text != null && MainActivity.tts != null) {
								Tts.speak(text);
							}

						}
					}, 2000);
				}
			}

		} else if ("MUSIC_PAUSE".equals(intent.getStringExtra(_CMD_))) {
			// 暂停音乐播放
		} else if ("MUSIC_RESUME".equals(intent.getStringExtra(_CMD_))) {
			// 可以开始音乐播放
		}
	}
}
